package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntCdbgmdtldBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntCdbgmdtldBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntCdbgmdtldBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntCdbgmdtldBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntCdbgmdtldBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土坍落度、观密度试验坍落度详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntCdbgmdtldBaDetailServiceImpl implements IReportSnhntCdbgmdtldBaDetailService {

    private final ReportSnhntCdbgmdtldBaDetailMapper baseMapper;

    /**
     * 查询水泥混凝土坍落度、观密度试验坍落度详情
     */
    @Override
    public ReportSnhntCdbgmdtldBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水泥混凝土坍落度、观密度试验坍落度详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntCdbgmdtldBaDetailVo> queryPageList(ReportSnhntCdbgmdtldBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntCdbgmdtldBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntCdbgmdtldBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土坍落度、观密度试验坍落度详情列表
     */
    @Override
    public List<ReportSnhntCdbgmdtldBaDetailVo> queryList(ReportSnhntCdbgmdtldBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntCdbgmdtldBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntCdbgmdtldBaDetail> buildQueryWrapper(ReportSnhntCdbgmdtldBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntCdbgmdtldBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntCdbgmdtldBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShyjbh()), ReportSnhntCdbgmdtldBaDetail::getShyjbh, bo.getShyjbh());
        lqw.eq(bo.getTanldcz1() != null, ReportSnhntCdbgmdtldBaDetail::getTanldcz1, bo.getTanldcz1());
        lqw.eq(bo.getTanldcz2() != null, ReportSnhntCdbgmdtldBaDetail::getTanldcz2, bo.getTanldcz2());
        lqw.eq(bo.getTanldpjz() != null, ReportSnhntCdbgmdtldBaDetail::getTanldpjz, bo.getTanldpjz());
        lqw.eq(bo.getKuozdcdz1() != null, ReportSnhntCdbgmdtldBaDetail::getKuozdcdz1, bo.getKuozdcdz1());
        lqw.eq(bo.getKuozdcdzz2() != null, ReportSnhntCdbgmdtldBaDetail::getKuozdcdzz2, bo.getKuozdcdzz2());
        lqw.eq(bo.getKuozdpjz() != null, ReportSnhntCdbgmdtldBaDetail::getKuozdpjz, bo.getKuozdpjz());
        lqw.eq(StringUtils.isNotBlank(bo.getBaosx()), ReportSnhntCdbgmdtldBaDetail::getBaosx, bo.getBaosx());
        lqw.eq(StringUtils.isNotBlank(bo.getHansqk()), ReportSnhntCdbgmdtldBaDetail::getHansqk, bo.getHansqk());
        lqw.eq(StringUtils.isNotBlank(bo.getNianjx()), ReportSnhntCdbgmdtldBaDetail::getNianjx, bo.getNianjx());
        lqw.eq(StringUtils.isNotBlank(bo.getGund()), ReportSnhntCdbgmdtldBaDetail::getGund, bo.getGund());
        return lqw;
    }

    /**
     * 新增水泥混凝土坍落度、观密度试验坍落度详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntCdbgmdtldBaDetailBo bo) {
        ReportSnhntCdbgmdtldBaDetail add = BeanUtil.toBean(bo, ReportSnhntCdbgmdtldBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土坍落度、观密度试验坍落度详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntCdbgmdtldBaDetailBo bo) {
        ReportSnhntCdbgmdtldBaDetail update = BeanUtil.toBean(bo, ReportSnhntCdbgmdtldBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntCdbgmdtldBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土坍落度、观密度试验坍落度详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
